﻿using System;
using System.Threading;
using System.Windows.Forms;
using LDSBusiness;
using LDSCore;
using LDSCore.Result;
using LDSModel.Dal;
using LDSProject.Properties;

namespace LDSProject.Forms
{
    public partial class LoginForm : Form
    {
        readonly MainForm _mainForm;
        bool _isSoftClose;
        bool _isInputPwd;
        Thread _thread;

        public LoginForm(MainForm form)
        {
            InitializeComponent();
            _mainForm = form;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            lbInfo.Visible = false;
            Text = Resources.ProjectName + @" -- 用户登录";
            lbTitle.Text = Resources.ProjectName;
            lbVersion.Text = Resources.Version;
            lbCopyright.Text = @"版权所有 : " + Resources.Company;

            tbUserName.Text = Settings.Default.UserName;
            ckJZPwd.Checked = Settings.Default.JZPwd;
            ckAutoLogin.Checked = Settings.Default.AutoLogin;

            if (Settings.Default.JZPwd)
            {
                tbPassword.Text = Settings.Default.Password;
                _isInputPwd = false;
            }

            if (!Settings.Default.AutoLogin || string.IsNullOrWhiteSpace(Settings.Default.UserName) ||
                string.IsNullOrWhiteSpace(Settings.Default.Password)) return;
            _thread = new Thread(AutoLogin);
            _thread.Start();
        }

        void AutoLogin()
        {
            for (var i = 0; i < 3; i++)
            {
                Invoke(new DelegateSetInfo(UpdateInfo), "登录中(" + (3 - i) + ")...", true);
                Thread.Sleep(1000);
                if (ckAutoLogin.Checked) continue;
                Invoke(new DelegateEmpty(HideInfo));
                return;
            }
            Invoke(new DelegateEmpty(AutoClick));
        }

        void UpdateInfo(string info, bool isOk)
        {
            lbInfo.Text = info;
            lbInfo.Visible = true;
        }

        void HideInfo()
        {
            lbInfo.Visible = false;
        }

        void AutoClick()
        {
            btnLogin.PerformClick();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (null != _thread)
            {
                try
                {
                    _thread.Abort();
                }
                catch
                { }
            }
            Login(tbUserName.Text.Trim(), tbPassword.Text.Trim());
        }

        #region 方法实现

        private bool CheckLogin(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                tbUserName.Focus();
                tbUserName.SelectAll();
                lbInfo.Text = Resources.UserNameIsNull;
                lbInfo.Visible = true;
                return false;
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                tbPassword.Focus();
                tbPassword.SelectAll();
                lbInfo.Text = Resources.PasswordIsNull;
                lbInfo.Visible = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        private void Login(string userName, string password)
        {
            if (CheckLogin(userName, password))
            {
                if (_isInputPwd) password = Utils.EncryptMd5(password);

                OperationResult result = UserService.Login(userName, password);

                if (result.Success)
                {
                    Settings.Default.UserName = userName;
                    Settings.Default.Password = password;
                    Settings.Default.Save();

                    _isSoftClose = true;
                    Close();
                    _mainForm.Reload();
                }
                else
                {
                    lbInfo.Text = result.Code.ToMessage();
                    lbInfo.Visible = true;
                }
            }
        }

        #endregion

        private void tbUserName_TextChanged(object sender, EventArgs e)
        {
            lbInfo.Visible = false;
        }

        private void tbPassword_TextChanged(object sender, EventArgs e)
        {
            lbInfo.Visible = false;
            _isInputPwd = true;
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_isSoftClose) Exit();
        }

        private void Exit()
        {
            Environment.Exit(0);
        }

        private void ckJZPwd_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.JZPwd = ckJZPwd.Checked;
            Settings.Default.Save();
        }

        private void ckAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.AutoLogin = ckAutoLogin.Checked;
            Settings.Default.Save();
        }
    }
}
